/**
 * 籌碼類
 * @author LuoDong
 * @email luo4521@online.net.tw
 * 2010-2-25 10:29
 */
package com.addcn.texas.utils
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.*;	
	import flash.utils.Timer;
	
	import com.addcn.common.utils.AppLibary;
	import caurina.transitions.Tweener;
	
	public class  Chips extends Sprite
	{
		public static const MOVE_EVENT : String = "moveEvent";
		public static const SHOW_EVENT : String = "showEvent";
		
		private var _x : int;
		private var _y : int;
		protected var _chip : int;
		private var _tf : TextField;		
		
		public function Chips(r_chip : int) : void {
			this._chip = r_chip;
			chipDisplay();		
		}
		
		private function _setTextFiled(r_txt : String) : void
		{
			if(this._tf == null){
				var _formatter:TextFormat = new TextFormat();			
				_formatter.size = 14;		
				_formatter.align = TextFormatAlign.CENTER;
				_formatter.bold = true;
				_formatter.color = 0xFFFF00;		
				_tf = new TextField;	
				_tf.name = "txt";
				_tf.autoSize = TextFieldAutoSize.CENTER;
				_tf.defaultTextFormat = _formatter;
				_tf.text = r_txt;	
				_tf.selectable = false;	
				_tf.y = 15;
				_tf.x = 0;	
				this.addChild(_tf);			
				
			}else {				
				this._tf.text = r_txt;
			}
		}
		
		//顯示籌碼
		public function show(r_parent : Object,r_shade : Boolean = false,r_delay:Number = 0) : void
		{
			if (this._chip == 0 ) {				
				return;
			}				
			this.x = this._x;
			this.y = this._y;			
			r_parent.addChild(this);	
			if (r_shade) {
				//this.alpha = 0;
			}
			if (r_shade) {				
				Tweener.addTween(this, {onStart:_alp, onComplete:_showCom, alpha:1, time:0.6 ,delay:r_delay} );
			}			
		}
		private function _alp() : void
		{
			this.alpha = 0;
		}
		private function _showCom(): void
		{
			var _event: Event = new Event(Chips.SHOW_EVENT);
			dispatchEvent(_event);
		}
		
		//設置籌碼位置
		public function setPosition(r_x : int, r_y:int ) : void
		{
			this._x = r_x;
			this._y = r_y;
			this.x = r_x;
			this.y = r_y;
		}
		
		/**
		 * 分割出筹码
		 * @param	r_chip int
		 */
		public  function cut(r_chip : int) : Chips
		{
			if (this._chip <= r_chip) {
				return this;
			}else{
				this._chip -= r_chip;
				chipDisplay();
				var _new_chips : Chips = new Chips(r_chip);
				_new_chips.setPosition(this.x, this.y);
				_new_chips.show(this.parent);
				return _new_chips;
			}
		}
		
		/**
		 * 加入其他籌碼
		 * @param	r_arr Array [Chips,...]
		 */
		public function add(r_arr : Array) : void
		{
			var _chips : Chips;
			var _num : int;
			for each(_chips in r_arr) {
				_num = _chips.amount;
				this._chip += _num;
				_chips.remove();
			}
			chipDisplay();
		}
			
		//更改籌碼
		public function modifyChip(r_num : int, r_delay:int = 0) : void
		{		
			this._chip = r_num;							
			chipDisplay();			
		}
	
		//獲取金額
		public function get amount() : int
		{
			return this._chip;
		}		
					
		//移除文字
		public function removeText() : void
		{
			if(this._tf != null){
				this._tf.parent.removeChild(this._tf);
				this._tf = null;
			}
		}
		
		//移動籌碼
		public function move(r_x : int, r_y : int ,r_delay : Number = 0) : void
		{							
			Tweener.addTween(this, { x:r_x, y:r_y, time:0.6, delay : r_delay, transition:"easeOutSine" ,onStart:_moveOnStart, onComplete:_complete } );							
		}
		
		private function _moveOnStart() : void
		{
			removeText();
		}
		
		private function _complete() : void
		{				
			Tweener.removeTweens(this, "x", "y");			
			remove();
			var _event : Event = new Event(MOVE_EVENT);							
			dispatchEvent(_event);			
		}
		
		//移除籌碼
		public function remove() : void
		{
			Tweener.addTween(this, { alpha:0, time:0.5 , onComplete:_clearComplete } );				
		}
		
		private function _clearComplete() : void
		{
			Tweener.removeTweens(this, "alpha");		
			if(this.parent){
				this.parent.removeChild(this);
			}
		}  
			
				           		
		/**
		 * 整理筹码，将金额转换为对应为等额的筹码
		 */
		private function chipDisplay() : void
		{			
			_setTextFiled(String(this.amount));
			var _str : String = this._chip.toString();
			var _chip_list : Object = {};
			var _num : int;
			var _len : int = _str.length - 1;
			var _index : int;
			for (var i=0; i<=_len ; i++) {
				var _pow = _len - i;
				var _limit =  Math.pow(10, _pow); 				
				_num = parseInt(_str.substr(i, 1));											
				 if (_num > 0) {
					if(_num > 5){
						_chip_list[5*_limit] = 1;
						_num = _num -5;
					}
					_chip_list[_limit] = _num;
				 }
				 
				 if(_pow ==0){
					  var _val = _chip_list[_limit];
					  if(_val ==2){
						   _chip_list[2] = 1;  
						   delete _chip_list[_limit];
					  }else if(_val >2){
					        _chip_list[2] = (_val /2) >>0;  
							if(_val - _chip_list[2]){
								delete _chip_list[_limit];
							}
					  }
				  }	
			}		
			
			_addChipMc(_chip_list);			
		}
		
		private function _addChipMc(r_list : Object) : void
		{		
			this._removeChild();
			var _class : Class;
			var _mc : DisplayObject;
			var _len : int;
			var _chip_x : int = 0;
			var _chip_y : int = 0;
			var _pos : int = 0;
			var _num : int = 0;
			for (var k in r_list) {				
				_class = AppLibary.getInstance().getClass("chip" + k);
				if (_class == null) return;
				_len = r_list[k];
				for (; _len > 0; _len-- ) {
					_num++;
					if (_num % 5 == 0) {							
						_pos++;
						_chip_x += 24;
						//if (_pos % 3 == 0) {
							//_chip_y += 24;
						//}else {
							//_chip_x += 24;
						//}
					}					
					//_chip_x++;
					_chip_y -= 3;
					_mc = new _class;
					_mc.x = Math.random() *3 + _chip_x;
					_mc.y = _chip_y - 6;
					this.addChild(_mc);				
				}
			}			
		}
		
		private function _removeChild() : void
		{
			var dis:DisplayObjectContainer
			for (var i:uint = 0; i < this.numChildren; i++) {				
				if (this.getChildAt(i) is DisplayObjectContainer) {
					dis = this.getChildAt(i) as DisplayObjectContainer;
					if(dis.name != "txt"){
						dis.parent.removeChild(dis);						
					}
				} 
			}
		}
		
	}
	
}
